Blockly.Python['hw_iot_package'] = function(block) {
    return "import libs.iot.hw as hwiot \n";
};

Blockly.Python['hw_iot_server_url'] = function(block) {
    return [Blockly.Python.quote_("iot-mqtts.cn-north-4.myhuaweicloud.com"), Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['hw_iot_client_id'] = function(block) {
    var device = Blockly.Python.valueToCode(block, 'DEVICE', Blockly.Python.ORDER_ATOMIC);
    return ["hwiot.create_client_id(" + device + ")", Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['hw_iot_encrypt_password'] = function(block) {
    var password = Blockly.Python.valueToCode(block, 'PASSWORD', Blockly.Python.ORDER_ATOMIC);
    return ["hwiot.encrypt_password(" + password + ")", Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['hw_iot_device_commad_topic'] = function(block) {
    var device = Blockly.Python.valueToCode(block, 'DEVICE', Blockly.Python.ORDER_ATOMIC);
    return ["hwiot.create_device_command_topic(" + device + ")", Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['hw_iot_device_report_topic'] = function(block) {
    var device = Blockly.Python.valueToCode(block, 'DEVICE', Blockly.Python.ORDER_ATOMIC);
    return ["hwiot.create_device_report_topic(" + device + ")", Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['hw_iot_device_report_service'] = function(block) {
    var lines = [];
    lines.push("{\"services\": [");

    var service = Blockly.Python.valueToCode(block, 'SERVICE', Blockly.Python.ORDER_ATOMIC);
    lines.push("{\"service_id\": " + service.replace(/\'/g, '"') + ", \"properties\": {");

    var ps = [];
    for (var i = 0; i < block.itemCount_; i++) {
        var p = block.getInput('PROPERTY' + i);
        var v = block.getInput('VALUE' + i);
        if (p && v) {
            ps.push(Blockly.Python.valueToCode(block, 'PROPERTY' + i, Blockly.Python.ORDER_ATOMIC).replace(/\'/g, '"')
                + ": "
                + Blockly.Python.valueToCode(block, 'VALUE' + i, Blockly.Python.ORDER_ATOMIC).replace(/\'/g, '"'));
        }
    }

    lines.push(ps.join(', '));
    lines.push("}}]}");

    return [Blockly.Python.quote_(lines.join('')), Blockly.Python.ORDER_ATOMIC];
};
